package com.heima.service.impl;

import com.heima.component.RedisComponent;
import com.heima.entity.constants.Constants;
import com.heima.entity.dto.UserSpaceDto;
import com.heima.entity.enums.FileDelFlagEnums;
import com.heima.entity.enums.FileFolderTypeEnums;
import com.heima.entity.po.FileInfo;
import com.heima.entity.po.UserInfo;
import com.heima.entity.query.FileInfoQuery;
import com.heima.entity.query.UserInfoQuery;
import com.heima.mapper.FileInfoMapper;
import com.heima.mapper.UserInfoMapper;
import com.heima.service.RecycleService;
import com.heima.service.UserInfoService;
import com.heima.utils.StringTools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class RecycleServiceImpl implements RecycleService {
    @Resource
    private FileInfoServiceImpl fileInfoService;
    @Resource
    private FileInfoMapper<FileInfo,FileInfoQuery> fileInfoMapper;
    @Resource
    private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;
    @Resource
    private RedisComponent redisComponent;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recoverFile(String userId, String fileIds) {
        String[] fileIdArray = fileIds.split(",");

        FileInfoQuery query = new FileInfoQuery();
        query.setFileIdArray(fileIdArray);
        query.setUserId(userId);
        query.setDelFlag(FileDelFlagEnums.RECYCLE.getFlag());

        List<FileInfo> fileInfoList = fileInfoService.findListByParam(query);

        List<String> fileIdList = new ArrayList<>();
        if (fileInfoList.isEmpty()){
            return;
        }
        for (FileInfo fileInfo : fileInfoList) {
            if (fileInfo.getFolderType() == FileFolderTypeEnums.FOLDER.getType()){
                fileInfoService.findAllFileInFolder(fileIdList,fileInfo.getFileId(),userId,FileDelFlagEnums.DEL.getFlag());
            }
            fileIdList.add(fileInfo.getFileId());
        }
        //查询所有根目录文件 防止名字重复
        query = new FileInfoQuery();
        query.setUserId(userId);
        query.setDelFlag(FileDelFlagEnums.USING.getFlag());
        query.setFilePid(Constants.ZERO_STR);
        List<FileInfo> list = fileInfoService.findListByParam(query);
        Map<String,FileInfo> fileInfoMap = list.stream().collect(Collectors.toMap(FileInfo::getFileName, Function.identity(),(data1, data2)->data2));

        //更新为使用中
        if (!fileIdList.isEmpty()){
            FileInfo fileInfo = new FileInfo();
            fileInfo.setDelFlag(FileDelFlagEnums.USING.getFlag());
            fileInfo.setLastUpdateTime(new Date());
            fileInfoMapper.updateMyFileDelFlagBatch(fileInfo,fileIdList,userId,null);
        }

        //如果选中的文件与移动的文件夹中的文件出现重名
        for (FileInfo fileInfo : fileInfoList) {
            FileInfo fileInfo1 = fileInfoMap.get(fileInfo.getFileName());
            FileInfo updateInfo = new FileInfo();
            if (fileInfo1 != null){
                String fileName = StringTools.rename(fileInfo.getFileName());
                updateInfo.setFileName(fileName);
                fileInfoMapper.updateByFileIdAndUserId(updateInfo,fileInfo.getFileId(),userId);
            }

        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delFile4ever(String userId, String fileIds) {
        String[] fileIdArray = fileIds.split(",");

        FileInfoQuery query = new FileInfoQuery();
        query.setFileIdArray(fileIdArray);
        query.setUserId(userId);
        query.setDelFlag(FileDelFlagEnums.RECYCLE.getFlag());

        List<FileInfo> fileInfoList = fileInfoService.findListByParam(query);

        List<String> fileIdList = new ArrayList<>();
        //找到子目录下的所有文件
        for (FileInfo fileInfo : fileInfoList) {
            if (fileInfo.getFolderType() == FileFolderTypeEnums.FOLDER.getType()){
                fileInfoService.findAllFileInFolder(fileIdList,fileInfo.getFileId(),userId,FileDelFlagEnums.DEL.getFlag());
            }
            fileIdList.add(fileInfo.getFileId());
        }
        //删除文件
        fileInfoMapper.deleteFileByFileIds(fileIdList,userId,2);

        //退回磁盘空间
        Long useSpace = fileInfoMapper.selectUseSpace(userId);
        UserInfo userInfo = new UserInfo();
        userInfo.setUseSpace(useSpace);
        userInfoMapper.updateByUserId(userInfo,userId);

        //设置缓存
        UserSpaceDto userSpaceDto = redisComponent.getUserSpaceUse(userId);
        userSpaceDto.setUseSpace(useSpace);
        redisComponent.saveUserSpace(userId,userSpaceDto);
    }
}
